#include "my_cache_conf.h"

#define DEF_NEVENTS                                 16384
#define DEF_WORKER_THREADS                          1
#define DEF_WORKER_MEM_POOL_WATER_LEVEL             16
#define DEF_ACCEPT_THREADS                          1
#define DEF_G_MEM_POOL_WATER_LEVEL                  32
#define DEF_G_ALIGNED_MEM_POOL_WATER_LEVEL          32
#define DEF_REQUEST_HEADER_BUF_SIZE                 4096
#define DEF_MAX_REQUEST_HEADER_SIZE                 8192
#define DEF_RESPONSE_HEADER_BUF_SIZE                4096
#define DEF_MAX_RESPONSE_HEADER_BUF_SIZE            8192
#define DEF_READ_REQUEST_HEADER_TIMEOUT_MS          1000
#define DEF_READ_RESPONSE_HEADER_TIMEOUT_MS         1000
#define DEF_BACKLOG                                 16384

static my_cache_conf_t g_cache_conf = {
    .inited = 0
};

void
my_http_listener_conf_init(my_http_listener_conf_t *cfg)
{
    my_conf_item_ptr_init(&cfg->listen_addr, MY_FALSE, NULL);
    my_conf_item_ptr_init(&cfg->scheme, MY_FALSE, NULL);
    my_conf_item_int64_init(&cfg->backlog, MY_TRUE, DEF_BACKLOG);
    my_conf_item_bool_init(&cfg->tcp_nodelay, MY_TRUE, MY_TRUE);
    my_conf_item_bool_init(&cfg->tcp_quicack, MY_TRUE, MY_FALSE);
    my_conf_item_bool_init(&cfg->reuseport, MY_TRUE, MY_FALSE);
    my_conf_item_bool_init(&cfg->reuseaddr, MY_TRUE, MY_TRUE);
    my_conf_item_bool_init(&cfg->ipv6only, MY_TRUE, MY_FALSE);
}

void
my_cache_conf_init()
{
    my_service_conf_t *service_conf = &g_cache_conf.service_conf;
    my_conf_item_int64_init(&service_conf->nevents, MY_TRUE, DEF_NEVENTS);
    my_conf_item_int64_init(&service_conf->worker_threads,
        MY_TRUE, DEF_WORKER_THREADS);
    my_conf_item_ptr_init(&service_conf->worker_affinity, MY_TRUE, "off");
    my_conf_item_int64_init(&service_conf->worker_mem_pool_water_level,
        MY_TRUE, DEF_WORKER_MEM_POOL_WATER_LEVEL);
    my_conf_item_int64_init(&service_conf->accept_threads,
        MY_TRUE, DEF_ACCEPT_THREADS);
    my_conf_item_int64_init(&service_conf->g_mem_pool_water_level,
        MY_TRUE, DEF_G_MEM_POOL_WATER_LEVEL);
    my_conf_item_int64_init(&service_conf->g_aligned_mem_pool_water_level,
        MY_TRUE, DEF_G_ALIGNED_MEM_POOL_WATER_LEVEL);

    my_http_conf_t *http_conf = &g_cache_conf.http_conf;
    my_conf_item_int64_init(&http_conf->request_header_buf_size,
        MY_TRUE, DEF_REQUEST_HEADER_BUF_SIZE);
    my_conf_item_int64_init(&http_conf->max_request_header_size,
        MY_TRUE, DEF_MAX_REQUEST_HEADER_SIZE);
    my_conf_item_int64_init(&http_conf->response_header_buf_size,
        MY_TRUE, DEF_RESPONSE_HEADER_BUF_SIZE);
    my_conf_item_int64_init(&http_conf->max_response_header_buf_size,
        MY_TRUE, DEF_MAX_RESPONSE_HEADER_BUF_SIZE);
    my_conf_item_int64_init(&http_conf->read_request_header_timeout_ms,
        MY_TRUE, DEF_READ_REQUEST_HEADER_TIMEOUT_MS);
    my_conf_item_int64_init(&http_conf->read_response_header_timeout_ms,
        MY_TRUE, DEF_READ_RESPONSE_HEADER_TIMEOUT_MS);
    http_conf->n_listeners = 0;
    http_conf->listeners = NULL;

    g_cache_conf.inited = 1;
}

my_cache_conf_t *
my_cache_conf_get()
{
    if (!g_cache_conf.inited) {
        MY_LOG_FATAL("cache conf no inited");
    }

    return &g_cache_conf;
}